function resize() {  
  var html = document.querySelector("html");  
  var wW = document.body.clientWidth || document.documentElement.clientWidth;  
  var ratio = wW / 1920;  
  html.style.fontSize = 18 * ratio + "px";  
}  
window.addEventListener("DOMContentLoaded", function() {   
  resize();  
});  
window.addEventListener("resize", resize);

// 获取元素的index
HTMLElement.prototype.index = function () {
  return index(this, 0);
  function index(e, i) {
    if (e.previousElementSibling === null) {
      return i;
    } else {
      return index(e.previousElementSibling, i + 1);
    }
  }
}

function getObjById(idStr){
  return document.getElementById(idStr);
}

// 移除某个焦点，添加某个焦点，oplType: 0：只remove，1：只add,2：先remove，再add
function setFocus(removeIdStr,reClsName,addIdStr,addClsName,oplType){
  var addDom = getObjById(addIdStr);
  var removeDom = getObjById(removeIdStr);
  if (oplType == 0) {
    if (removeDom) {
      removeClass(removeDom,reClsName);
    }
  }else if (oplType == 1) {
    if (addDom) {
      addClass(addDom,addClsName);
    }
  }else {
    if (addDom && removeDom) {
      removeClass(removeDom,reClsName);
      addClass(addDom,addClsName);
    }
  }
}

// 获取地址栏url的参数
function getParam(paramName) {
  paramValue = "";
  isFound = false;
  if (this.location.search.indexOf("?") == 0 && this.location.search.indexOf("=") > 1) {
    arrSource = unescape(this.location.search).substring(1, this.location.search.length).split("&");
    i = 0;
    while (i < arrSource.length && !isFound) {
      if (arrSource[i].indexOf("=") > 0) {
        if (arrSource[i].split("=")[0].toLowerCase() == paramName.toLowerCase()) {
          paramValue = arrSource[i].split("=")[1];
          isFound = true;
        }
      }
      i++;
    }
  }
  return paramValue;
}


// 周六
function getCode (ev) {
  return ev.keyCode || ev.which || ev.charCode
}

function getIcanVal (el) {
  return el.getAttribute('ican')
}

function getElById (id) {
  return document.getElementById(id)
}

// 选中的样式是这样的 sel acti
// 按下方向键获取下一个元素
function getNextEl (code) {
  var el = getNextEl.curEl,
      _parent,
      ican,
      skip,
      dir,
      _el = el,
      rowCount = el.parentNode.getAttribute("ican-count")

  switch (code) {
    case 37:
      console.log('向左')
      // 要删除记住的

      if (!el || getIcanVal(el.parentNode) === 'col' || !(el = prevSibling(el))) {
        var _id = _el.parentNode.getAttribute('ican-left')
        el = _id ? intoNextById(_id) : null
      }

      break
    case 38:
      console.log('向上')
      // 要删除记住的
      rowCount = el.parentNode.getAttribute("ican-count")

      if (rowCount) {
        el = getPreIndex(el, rowCount)

        if (!el) {
          var _id = _el.parentNode.getAttribute('ican-top')
          el = _id ? intoNextById(_id) : null
        }

      } else if (getIcanVal(el.parentNode) === 'row' || !(el = prevSibling(el))) {
        var _id = _el.parentNode.getAttribute('ican-top')
        el = _id ? intoNextById(_id) : null
      }

      // if (!el) {
      //   var _id = _el.parentNode.getAttribute('ican-top')
      //   el = getElById(_id)
      // }
      break
    case 39:
      console.log('向右')
      // 要添加记住的
      // if (getIcanVal(el.parentNode) === 'row') {
      //   el = nextSibling(el)
      // }
      
      if (!el || getIcanVal(el.parentNode) === 'col' || !(el = nextSibling(el))) {
        var _id = _el.parentNode.getAttribute('ican-right')
        console.log(_id)
        el = _id ? intoNextById(_id) : null
      }
      break
    case 40:
      console.log('向下')
      // 要添加记住的
      if (rowCount) {
        el = getNextIndex(el, rowCount)

        if (!el) {
          // 这是在离开
          var _id = _el.parentNode.getAttribute('ican-bottom')
          // 这是进入
          el = _id ? intoNextById(_id) : null
        }
      } else if (getIcanVal(el.parentNode) === 'row' || !(el = nextSibling(el))) {
        // 这是在离开
        var _id = _el.parentNode.getAttribute('ican-bottom')
        // 这是进入
        el = _id ? intoNextById(_id) : null
      } 
      
      break
  }

  if (!el) {
    el = null
  }

  return el
}

function intoNextById (id) {
  var el = getElById(id),
      child = el.parentNode.children,
      index

  if (index = el.parentNode.getAttribute('ican-cur')) {
    return child[+index]
  }

  return el
}

// 这个需要修改
function getPreIndex (el, rowNum) {
  var child = Array.prototype.slice.call(el.parentNode.children),
      index = child.indexOf(el), 
      el = null,
      rowNum = (+rowNum)
  if (index === -1) return null
  return index - rowNum >= 0 ? (el = child[index - rowNum]) : null
}
function getNextIndex (el, rowNum) {
  var child = Array.prototype.slice.call(el.parentNode.children),
      index = child.indexOf(el), 
      el = null,
      rowNum = (+rowNum)

  if (index === -1) return null
  return index + rowNum < child.length ? (el = child[index + rowNum]) : null
}

// 寻找下一个子元素
// 如果有最后一个元素表示在一个元素内移动
function getNextChild (code, el, dir, rowCount) {
  var _nextChild,
      skip,
      icanDir = getIcanVal(el.parentNode)
  // 这种情况是在一个盒子里面，暂时不要skip了
  if (rowCount) {
    var allChilds = Array.prototype.slice.call(el.parentNode.children)
    var index = allChilds.indexOf(el) + rowCount * dir

    _nextChild = allChilds[index]

    if (_nextChild) {
      return _nextChild
    } else {
      el = getParentIcan(getParentIcan(el.parentNode)) // 这个parentNode有问题
      //return getNextChild(code, el.parentNode.parentNode, dir)
      // return null
    }
  }else if (((code == '37') && icanDir == 'col') || ((code == '38') && icanDir == 'row')) {
    // 这种是同级别跳转，还有一种跨父元素跳转
    el = getParentIcan(el, -1)
  } else if ((code == '39' && icanDir == 'col') || (code == '40' && icanDir == 'row')) {
    el = getParentIcan(el, 1)
  }

  if (dir > 0) {
    _nextChild = nextSibling(el)
  } else {
    _nextChild = prevSibling(el)
  }
  if (_nextChild === null) {
    if (el.parentNode.tagName.toLowerCase() === 'html') {
      return null
    }
    // 这里表示没有下一个了，要向上跳了
    // 这种情况只在向上的时候
    if (code == '38') {
      return getNextChild(code, getParentIcan(getParentIcan(el)), dir)
    }

    return null
    
  } else {
    return _nextChild
  }
}

// 获取父元素直到ican不是thr，这个函数有问题
function getParentIcan (el, dir) {
  el = el.parentNode

  function geta () {
    var a
    if (dir > 0) {
      a = nextSibling(el)
    } else if (dir < 0) {
      a = prevSibling(el)
    } else {
      a = hasSibling(el)
    }

    return a
  }

  while (!geta()) {
    el = el.parentNode
  }

  return el
}

function hasSibling (el) {
  var allSib = Array.prototype.slice.call(el.parentNode.children),
      nextIndex = 0,
      len
  allSib = allSib.filter(function (item) {
    return item !== el
  })

  for (nextIndex, len = allSib.length; nextIndex < len; nextIndex++) {
    if (getIcanVal(allSib[nextIndex])) {
      return allSib[nextIndex]
    }
  }
  // 随便返回一个
  return null
}

function nextSibling (el) {
  var allSib = Array.prototype.slice.call(el.parentNode.children),
      allSib = allSib.filter(function (item) {return item.getAttribute('ican')}),
      nextIndex = allSib.indexOf(el) + 1,
      len

  return allSib[nextIndex] ? allSib[nextIndex] : null
}

function prevSibling (el) {
  var allSib = Array.prototype.slice.call(el.parentNode.children),
      allSib = allSib.filter(function (item) {return item.getAttribute('ican')}),
      nextIndex = allSib.indexOf(el) - 1,
      len
  return allSib[nextIndex] ? allSib[nextIndex] : null
}

function findFirstIcan (el, rev) {

  var els = Array.prototype.slice.call(el.children),
      els = rev ? els.reverse() : els,
      i,
      len

  for (i = 0, len = els.length; i < len; i++) {
    if (getIcanVal(els[i]) !== 'true') {
      return findFirstIcan(els[i], rev)
    } else {
      return els[i]
    }
  }
}

// 寻找父元素
function getParent (el) {
  var _parent = el.parentNode
  if (getIcanVal(_parent) !== 'thr'){
    return {root: _parent, parent: el}
  }
  return getParent(_parent)
}

function setInitEl () {
  getNextEl.curEl = document.querySelector('.header_only_foucused_li')
}

function toNext () {

}

getNextEl.curEl = null

// 修改类
var idMap = {
  'album-s': 'sel',
  'album-f': 'sel',
  'nav': 'header_selected_li',
  'index': 'sel acti',
  'album-f': 'sel acti',
  'lei': 'tabacti',
  'albums': 'albumItem-acti focus',

  'hot-content': 'item-active focus',
  'matchFirRow': 'spArea_active'
}

var dirMap = {
  '37': -1,
  '39': 1
}

var idFnMap = {
  matchFirRow: function (el, code) {
    var curIndex = Array.prototype.slice.call(el.parentNode.children).indexOf(el)
    var moveNum = (curIndex) % 8 - 4 + 1

    if (moveNum < 0) {
      return
    }

    moveNum *= -1

    getObjById('matchFirRow').style.marginLeft = (24.556*moveNum) + 'em';
  }
}

var actMap = {
  leftMenuUl: function (curEl) {
    var leftMenu = document.getElementById("leftMenuUl")
    var liss = Array.prototype.slice.call(document.querySelectorAll('#leftMenuUl li'))
    var cons = Array.prototype.slice.call(document.querySelectorAll('.classJS'))
    var index = liss.indexOf(curEl)
    console.log(index)
    
    liss.forEach(function (item, _index) {
      // left_menu_active left_menu_only_focused
      if (index !== _index) {
        removeClass(item, 'left_menu_active left_menu_only_focused')
        
      } else {
        addClassV2(item, 'left_menu_active left_menu_only_focused')
        leftMenu.setAttribute('ican-right', curEl.getAttribute('ican-right'))
      }
    })

    cons.forEach(function (item, _index) {
      if (index !== _index) {
        item.style.display = 'none'
      } else {
        item.style.display = 'block'
      }
    })
  },
  default: function (curEl) {
    var _href = curEl.getAttribute('href')
    location.href = _href
  }
}

function handleAct (curEl) {
  var _id = curEl.parentNode.id
  if (_id in actMap) {
    actMap[_id](curEl)
  } else {
    actMap.default(curEl)
  }
}

function handleFocus (curEl, prevEl, code) {
  var curParentId = curEl.parentNode.id,
      prevParentId = prevEl.parentNode.id

  if (curParentId in idFnMap) {
    idFnMap[curParentId](curEl, code)
  }

  // if (prevParentId in idFnMap) {
  //   idFnMap[prevParentId](prevEl, code)
  // }

  addClassV2(curEl, idMap[curParentId] || 'act')

  if (curParentId in idMap) {
    // 头部元素
    
  }

  removeClass(prevEl, idMap[prevParentId] || 'act')

  // if (prevParentId in idMap) {
  //   // 头部元素
  //   removeClass(prevEl, idMap[prevParentId])
  // }

}

